Ext.namespace("Project.Draft");

var Draft = function(){
	
	var ps = new ProjectSource();
	var draftStore = new Ext.data.JsonStore({
	    url: '../project/searchProjects.form?status=100&timesnap='+new Date().getTime(),
	    root: 'result',
	    fields: ['PROJECT_ID',
	             'PROJECT_NAME',
	             'AREA_ID',
	             'PROJECT_TYPE',
	             'EST_AMT',
	             'PROJECT_CREATE_DT',
	             'EST_DT',
	             'JOIN_LEADER',
	             'CUSTOMER_NAME',
	             'STAFF_NUM',
	             'CONTACT_PERSON',
	             'OFF_TEL',
	             'MOBILE',
	             'EMAIL',
	             'PROJECT_TARGET',
	             'CUST_REQ',
	             'SERVICE_REQ',
	             'IMP_FUNC',
	             'OWNER_PROVIDE',
	             'REMARK',
	             'COMPETITOR',
	             'PROJECT_OWNER',
	             'INVOLVE_DT',
	             'ADVANTAGE',
	             'DISADVANTAGE',
	             'CREATE_USER',
	             'CREATE_DT',
	             'MOD_USER',
	             'MOD_DT' 
	             ]       
	});
	var draftGrid = new Ext.grid.GridPanel({
		width : 930,
		height : 160,
		store : draftStore,
		listeners : {
			rowmousedown : function(grid,index,e){
				var data = grid.store.getAt(index).data;
				ps.setCompValue(data);
				ps.projectIsVs.setValue(true);
			}
		},
		columns : [{header:'项目编号',width: 150,dataIndex:'PROJECT_ID'},
		           {header:'项目名称',width: 100, sortable: true,dataIndex:'PROJECT_NAME'},
		           {header:'所属区域',dataIndex:'AREA_ID',renderer:function(value,row){return ps.getGridAreaMap()[value];}},
		           {header:'项目类型',renderer:ps.renderprojectType,dataIndex:'PROJECT_TYPE'},
		           {header:'业务提供',dataIndex:'OWNER_PROVIDE'},
		           {header:'开始时间',dataIndex:'PROJECT_CREATE_DT'},
		           {header:'计划结束时间',dataIndex:'EST_DT'},
		           {header:'估算金额',renderer: 'usMoney',dataIndex:'EST_AMT'}],
       bbar:new Ext.PagingToolbar({
		 pageSize: 5,
         store: draftStore,
         displayInfo: true,
         displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
         emptyMsg: "没有记录..."
       })
	});
	this.draftPanel = new Ext.form.FormPanel({
		frame : true,
		width : 950,
		labelWidth : 85,
		scope : this,
		buttons : [{text:'保存',iconCls: 'icon-add',scope:this,handler:function(){draftSave(this.draftPanel,'00');}},
		           {text:'保存并提交',iconCls: 'icon-accept',scope:this,handler:function(){draftSave(this.draftPanel,'10');}},
		           {text:'重置',iconCls: 'icon-return',scope:this,handler:function(){this.draftPanel.getForm().reset();}},
		           {text:'删除',iconCls: 'icon-delete',scope:this,handler:function(){doDelete(this.draftPanel);}}
		           ],
		items : [draftGrid,ps.projectInfo,ps.projectNeed,ps.projectIsVs,ps.projectVsInfo],
 	    listeners: {
 	    	render: {fn:function(){ 
						init();
 	    			}}
 	    }
	});
	//保存立项信息路径
	var addCon = new Ext.data.Connection({
		url:'../project/saveProject.form?timesnap=' + new Date().getTime(),
		method:'post'
	});
	var draftSave = function(dp,status){
		if(!dp.getForm().isValid()){
			Utils.info("有必填项没有填写，请检查！","系统提示");
			return false;
		}
		Utils.request(addCon,{
			params:ps.getCompValue(status),
			success:function(data){
				if(data.result=='true'){
					Utils.info("操作成功","系统提示");	
					draftStore.reload();
					if(status==10){
						dp.getForm().reset();
					}
				}else{
					Utils.info("操作失败","系统提示");	
				}
			}
		});
	};
	init  = function(){
		draftStore.load();
		ps.getGridArea.load();
	};
	//保存立项信息路径
	var delCon = new Ext.data.Connection({
		url:'../project/delProject.form?timesnap=' + new Date().getTime(),
		method:'post'
	});
	var doDelete = function(dp){
		var projectId = ps.getProjectId();
		if(projectId!=''){
	        Utils.confirm2({msg:"您确实要删除吗？",
	        	width:220,
	        	fn:function(){
						Utils.request(delCon,{
							params:{
								project_id:projectId
							},
							success:function(data){
								if(data.result=='true'){
									Utils.info("删除成功","系统提示");	
									dp.getForm().reset();
									draftStore.reload();
									
								}else{
									Utils.info("删除失败","系统提示");	
								}
							}
						});
			       }
		    });

		}else{
			Utils.info("请选择确定要删除的项目","系统提示");	
		}
	};
};